//
//  XJMoreViewController.m
//  XinJiangMall
//
//  Created by Tzyang on 2018/4/3.
//  Copyright © 2018年 Tzyang. All rights reserved.
//

#import "XJMoreViewController.h"

#import "BaseTableViewCell.h"

#import <objc/message.h>

#import "XJLoginViewController.h"
#import "XJFeedbackViewController.h" // 意见反馈
#import "XJAboudUSViewController.h" // 关于我们
#import "XJWebViewController.h"


@interface XJMoreViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    ///在线客服电话号码
    NSString *_onlineServicePhone;
}
@property (nonatomic, strong) NSArray * xjm_cellArray;

@end

@implementation XJMoreViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navTitle = @"更多";
   // self.navButtonRight.hidden = YES;

    [self initSubviews];
    
    [self requestOnLinePhone];
}

//- (void)viewWillAppear:(BOOL)animated{
//    [super viewWillAppear:animated];
////[self.navigationController setNavigationBarHidden:NO animated:YES];
//    self.tabBarController.tabBar.hidden=YES;
//}

- (UITableView *)xjm_tableView{
    if (!_xjm_tableView) {
        UITableView * xjm_tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KWidth, KHeight) style:UITableViewStylePlain];
        self.xjm_tableView = xjm_tableView;
        [self.view addSubview:xjm_tableView];
    }
    return _xjm_tableView;
}

//- (XJMoreFooterView *)xjm_footerV{
//    if (!_xjm_footerV) {
//        XJMoreFooterView * xjm_footerV = [[XJMoreFooterView alloc]initWithFrame:CGRectMake(0, 0, KWidth, 100)];
//        self.xjm_footerV = xjm_footerV;
//        [self.view addSubview:xjm_footerV];
//    }
//    return _xjm_footerV;
//
//}

- (NSArray *)xjm_cellArray{
    
    if (!_xjm_cellArray) {
        _xjm_cellArray = @[
                           @[
                                @[@"配送规则",@"xjm_rule"],
                                @[@"提现账户管理",@"xjm_management"],
                                @[@"意见反馈",@"xjm_feedBack"],
                                @[@"在线客服",@"xjm_service"],
                                @[@"关于我们",@"xjm_about"],
                                @[@"用户协议",@"xjm_userProtocol"]
                                ],
                           @[
                                @[@"退出登录",@"xjm_userLogout"]
                                ]
                           ];
    }
    return _xjm_cellArray;
}

- (void)initSubviews{
    
    self.xjm_tableView.backgroundColor                  =  XJCOLOR_RGB(237, 237, 237);
    self.xjm_tableView.showsVerticalScrollIndicator     =  NO;
    self.xjm_tableView.showsHorizontalScrollIndicator   =  NO;
    
    self.xjm_tableView.delegate        =  self;
    self.xjm_tableView.dataSource      =  self;
    
  //  self.xjm_tableView.tableFooterView =  self.xjm_footerV;
    
    [self setExtraCellLineHidden:self.xjm_tableView];
    
}

#pragma mark tableView datasource && delegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.xjm_cellArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return [self.xjm_cellArray[section] count];
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
        BaseTableViewCell * bCell = [BaseTableViewCell cellWithTableView:tableView];
        bCell.selectionStyle      = UITableViewCellSelectionStyleGray;
        NSArray *rows             = self.xjm_cellArray[indexPath.section][indexPath.row];
        bCell.baseTitle           = [rows firstObject];
    
    if (indexPath.section==1) {
        bCell.selectionStyle        =   UITableViewCellSelectionStyleNone;
        bCell.baseIndicator.hidden  =   YES;
    }
        return bCell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return ADAPTHeight(50);
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
        NSArray *array   = self.xjm_cellArray[indexPath.section][indexPath.row];
        
        NSString *item   = [array lastObject];
        
        SEL cellSelector = NSSelectorFromString(item);
        
        if ([self respondsToSelector:cellSelector]) {
            ((void (*)(id, SEL))objc_msgSend)(self,cellSelector);
        }
    
}


//设置分割线
- (void)tableView:(UITableView *)tableView willDisplayCell:(nonnull UITableViewCell *)cell forRowAtIndexPath:(nonnull NSIndexPath *)indexPath{

    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];

        if (indexPath.row < self.xjm_cellArray.count) {
            //四个参数：上左下右
            cell.separatorInset=UIEdgeInsetsMake(0, 15, 0, 15);
        }
    }
    // tableView margin settings
    if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
        [cell setPreservesSuperviewLayoutMargins:NO];
    }
    // 设置 cell  layout margins
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }

}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    if (section==1) {
        return ADAPTHeight(20);
    }else{
        
        return 0;
    }
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KWidth, ADAPTHeight(20))];
    
    UIView * backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KWidth, ADAPTHeight(20))];
    [view addSubview:backView];
    backView.backgroundColor = XJCOLOR_RGB(237, 237, 237);
    return view;
}

//隐藏多余分割线
-(void)setExtraCellLineHidden: (UITableView *)tableView{
    
    UIView *view = [UIView new];
    
    view.backgroundColor = [UIColor clearColor];
    
    [tableView setTableFooterView:view];
    
}


#pragma mark cell 点击

- (void)xjm_rule{
    
    XJLog(@"点击配送规则");
//    [JRToast showWithText:@"点击配送规则"];

    XJWebViewController *webVc = [[XJWebViewController alloc] init];
    webVc.vcType = XJWebVCTypeDeliveryRule;
    [self.navigationController pushViewController:webVc animated:YES];
}

- (void)xjm_management{
    
    XJLog(@"点击体现账户管理");
    [JRToast showWithText:@"点击体现账户管理"];
}

- (void)xjm_feedBack{
    
    XJLog(@"点击意见反馈");
    
    XJFeedbackViewController *feedbackVC = [XJFeedbackViewController new];
    [self.navigationController pushViewController:feedbackVC animated:YES];
    
}

- (void)xjm_service{
    
    XJLog(@"点击在线客服");

    [self callPhone];
}
- (void)callPhone {
    
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"联系客服" message:@"确定要拨打客服电话?" preferredStyle:(UIAlertControllerStyleAlert)];
    
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:nil];
    
    UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        
        NSString *phone = _onlineServicePhone;
        if ([NSString isEmptyString:_onlineServicePhone]) {
            phone = @"10086";
        }
        
        NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"tel:%@",phone];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
    }];
    
    [alertVC addAction:cancel];
    [alertVC addAction:sure];
    
    [self presentViewController:alertVC animated:YES completion:nil];
}

- (void)xjm_about{
   
    XJLog(@"点击关于我们");
//    XJAboudUSViewController *aboutUSVC = [[XJAboudUSViewController alloc] initWithNibName:@"XJAboudUSViewController" bundle:nil];
//    [self.navigationController pushViewController:aboutUSVC animated:YES];
    XJWebViewController *webVc = [[XJWebViewController alloc] init];
    webVc.vcType = XJWebVCTypeAboutUs;
    [self.navigationController pushViewController:webVc animated:YES];

    
}

- (void)xjm_userProtocol{
    
    XJLog(@"点击用户协议");
//    [JRToast showWithText:@"点击用户协议"];
    
    XJWebViewController *webVc = [[XJWebViewController alloc] init];
    webVc.vcType = XJWebVCTypeUSerProtocol;
    [self.navigationController pushViewController:webVc animated:YES];
}


- (void)xjm_userLogout{
    
    XJLog(@"点击退出登录");
    
    [XJUserModel xj_loginOutClearData];
    
    XJLoginViewController * xjLoginVC = [[XJLoginViewController alloc]init];
//    [self.navigationController pushViewController:xjLoginVC animated:YES];
    UINavigationController *navc = [[UINavigationController alloc] initWithRootViewController:xjLoginVC];
    [self presentViewController:navc animated:YES completion:nil];
}



#pragma mark - 五 获取在线客服电话
- (void)requestOnLinePhone {
    [XJHttpTool requestOnlineServicePhoneSuccessH:^(id responseObjc) {
        
        NSString *code = [NSString stringWithFormat:@"%@",responseObjc[@"code"]];
        if ([code isEqualToString:@"0"]) {
            _onlineServicePhone = responseObjc[@"data"][@"phone"];
        }
    } failH:^(NSError *error) {
        XJLog(@"获取在线客服电话号码失败:%@",error);
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
